package com.schillerchina.baselibrary.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.util.List;


/**
 * @author zhangzhiguo
 * @date 2019-09-16 10:16
 * @Description: BLE蓝牙选择Dialog
 */
public class Pref {

    private static SharedPreferences pref;

    public static void init(Context context) {
        pref = PreferenceManager.getDefaultSharedPreferences(context);
    }

    /**
     * @author Liu Huanbin
     * @date 2018/9/13
     * @Description: 本地缓存数据的常量key，用于存储及取出缓存数据
     */
    private static class Key {
        private static class Network {
            private static final String TOKEN = "sp_token";
            private static final String MASTER_SERVER_ROOT = "sp_master_server_root";
            private static final String WEB_SOCKET_ROOT = "sp_web_socket_root";
            public static final String MASTER_SERVER_IP = "masterServerIp";
            public static final String MASTER_SERVER_PORT = "masterServerPort";
        }

        private static class User {
            private static final String USER_INFO = "user_info";
            private static final String USER_NAME_HISTORY = "sp_user_name_history";
        }

        private static class Version {
            private static final String VERSION_MESSAGE = "sp_server_version_information";
            private static final String DOWNLOAD_APK_ID = "sp_download_apk_file_id";
        }

        private static class ImgPath {
            private static final String IMAGE_PATH = "sp_image_prefix_path";
            private static final String LARGE_IMAGE_PATH = "sp_large_image_prefix_path";
        }
    }

    //获取主服务器IP地址
    public static String getMasterServerIp() {
        return getString(Key.Network.MASTER_SERVER_IP, "");
    }

    public static void saveMasterServerIp(String ip) {
        put(Key.Network.MASTER_SERVER_IP, ip);
    }

    public static void saveMasterServerPort(String port) {
        put(Key.Network.MASTER_SERVER_PORT, port);
    }

    //获取主服务器端口号
    public static String getMasterServerPort() {
        return getString(Key.Network.MASTER_SERVER_PORT, "");
    }

    public static void put(String key, int value) {
        pref.edit().putInt(key, value).apply();
    }

    public static void put(String key, long value) {
        pref.edit().putLong(key, value).apply();
    }

    public static void put(String key, String value) {

        pref.edit().putString(key, value).commit();
    }
    public static void putApplye(String key, String value) {

        pref.edit().putString(key, value).apply();
    }

    private static void putCommit(String key, String value) {
        pref.edit().putString(key, value).commit();
    }

    public static void put(String key, boolean value) {
        pref.edit().putBoolean(key, value).apply();
    }

    public static void put(String key, float value) {
        pref.edit().putFloat(key, value).apply();
    }

    public static void remove(String key) {
        pref.edit().remove(key).commit();
    }

    public static String getString(String key, String def) {
        return pref.getString(key, def);
    }

    public static int getInt(String key, int def) {
        return pref.getInt(key, def);
    }

    public static long getLong(String key, long def) {
        return pref.getLong(key, def);
    }

    public static float getFloat(String key, float def) {
        return pref.getFloat(key, def);
    }

    public static boolean getBoolean(String key, boolean def) {
        return pref.getBoolean(key, def);
    }


    /**
     * 获取server地址
     *
     * @return 返回本地保存的server地址信息，否则返回null
     */
    public static String getMasterServerRoot() {
        return getString(Key.Network.MASTER_SERVER_ROOT, "");
    }

    /**
     * 保存server地址信息到本地缓存， server信息包括IP:Port 例如 192.168.5.61:9090
     *
     * @param serverAdd server地址
     */
    public static void saveMasterServerRoot(String serverAdd) {
        put(Key.Network.MASTER_SERVER_ROOT, serverAdd);
    }

    /**
     * 获取web socket地址根目录
     *
     * @return 返回本地保存的websocket地址根目录信息，否则返回null
     */
    public static String getWebSocketRoot() {
        return getString(Key.Network.WEB_SOCKET_ROOT, "");
    }

    /**
     * 保存websocket根目录地址信息到本地缓存
     *
     * @param socketRoot socket 根目录地址
     */
    public static void saveWebSocketRoot(String socketRoot) {
        put(Key.Network.WEB_SOCKET_ROOT, socketRoot);
    }

    /**
     * 获取server API 请求地址
     *
     * @return 带http协议的接口地址，http://192.168.5.61:9090
     */
    public static String getMasterServerAddress() {
        return "http://".concat(getMasterServerIp()).concat(Symbols.COLON)
                .concat(getMasterServerPort()).concat(getMasterServerRoot());
    }

    /**
     * 获取协议头server API 请求地址
     *
     * @param isHttps 是否携带加密证书协议头
     * @return 带http协议的接口地址，http://192.168.5.61:9090
     */
    public static String getMasterServerAddress(boolean isHttps) {
        if (isHttps) {
            return "https://".concat(getMasterServerIp()).concat(Symbols.COLON)
                    .concat(getMasterServerPort()).concat(getMasterServerRoot());
        }
        return getMasterServerAddress();
    }

    /**
     * 获取WebSocket Server地址
     *
     * @return 带ws协议的订阅地址，ws://192.168.5.61:9090/network-websocket/websocket
     */
    public static String getWebSocketAddress() {
        return "ws://".concat(getMasterServerIp()).concat(Symbols.COLON).concat(getMasterServerPort())
                .concat(getMasterServerRoot()).concat("network-websocket/websocket");
    }

    /**
     * 获取WebSocket Server地址
     *
     * @return 带ws协议的订阅地址，ws://192.168.5.61:9090/network-websocket/websocket
     */
    public static String getWebSocketCommonAddress() {
        return "ws://".concat(getMasterServerIp()).concat(Symbols.COLON).concat(getMasterServerPort())
                .concat(getMasterServerRoot()).concat("common-server/network-websocket/websocket");
    }

    /**
     * websocket 服务器地址，
     *
     * @param isHttps 是否安全加密服务器
     * @return
     */
    public static String getWebSocketAddress(Boolean isHttps) {
        if (isHttps) {
            return "wss://".concat(getMasterServerIp()).concat(Symbols.COLON)
                    .concat(getMasterServerPort()).concat(getWebSocketRoot());
        }
        return "ws://".concat(getMasterServerIp()).concat(Symbols.COLON)
                .concat(getMasterServerPort()).concat(getWebSocketRoot());
    }

    /**
     * 获取本地存储的token
     *
     * @return 返回本地缓存的token，若未缓存则返回null
     */
    public static String getSavedToke() {
        return getString(Key.Network.TOKEN, "");
    }

    public static void saveToken(String token) {
        put(Key.Network.TOKEN, token);
    }

    /**
     * 获取本地缓存的User信息，User对象转为json字符串后缓存到本地
     *
     * @return user对象json字符串
     */
    public static String getUserJson() {
        return getString(Key.User.USER_INFO, "");
    }

    public static void saveUserJson(String userJson) {
        putCommit(Key.User.USER_INFO, userJson);
    }

    /**
     * 清除本地用户信息缓存（同步方法，用于退出登陆调用)
     */
    public static void clearUserJson() {
        remove(Key.User.USER_INFO);
    }

    /**
     * 获取当前缓存的用户名列表
     *
     * @return
     */
    public static List<String> getRecordUserName() {
        String name = getString(Key.User.USER_NAME_HISTORY, null);
        if (!StringUtils.isEmpty(name)) {
            return StringUtils.splitToArray(name);
        } else {
            return null;
        }
    }

    /**
     * 保存要缓存的登录的用户名
     *
     * @param name
     */
    public static void saveUserName(String name) {
        List<String> nameList = getRecordUserName();
        if (nameList == null) {
            put(Key.User.USER_NAME_HISTORY, name);
        } else {
            nameList.remove(name);
            nameList.add(0, name);
            put(Key.User.USER_NAME_HISTORY, StringUtils.join(nameList));
        }
    }

    /**
     * 删除登陆历史列表中的用户名
     *
     * @param name 删除指定的用户名
     */
    public static void deleteUserName(String name) {
        List<String> list = getRecordUserName();
        if (list != null) {
            list.remove(name);
            putCommit(Key.User.USER_NAME_HISTORY, StringUtils.join(list));
        }
    }

    /**
     * 保存静态图片服务地址前缀
     *
     * @param path
     */
    public static void saveImagePath(String path) {
        put(Key.ImgPath.IMAGE_PATH, path);
    }

    /**
     * 获取图片静态路径，默认“/”
     *
     * @return
     */
    public static String getImagePath() {
        return getString(Key.ImgPath.IMAGE_PATH, Symbols.SLASH);
    }

    /**
     * 获取静态图片服务地址
     *
     * @return
     */
    public static String getImageUrl() {
        return getMasterServerAddress() + getImagePath();
    }

    /**
     * 保存静态大图片服务地址前缀
     *
     * @param path
     */
    public static void saveLargeImagePath(String path) {
        put(Key.ImgPath.LARGE_IMAGE_PATH, path);
    }

    /**
     * 获取大图片静态路径，默认“/”
     *
     * @return
     */
    public static String getLargeImagePath() {
        return getString(Key.ImgPath.LARGE_IMAGE_PATH, Symbols.SLASH);
    }
}
